package xframe.common;

import java.io.*;
import java.util.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

// Referenced classes of package xframe.common:
//            StartClass

public class StartServlet extends HttpServlet
{

    public StartServlet()
    {
    }

	public void init(ServletConfig conf)
        throws ServletException
    {
        super.init(conf);
        if(initialized)
            return;
        initialized = true;
        String CONF_PATH = getInitParameter("confFile");
        try
        {
            System.out.println("=========================================================================");
            System.out.println("                  WELCOME X-FrameWorks ver 2.0");
            System.out.println(" modified  2007.8.25 ");
            System.out.println(" since 2005.07       ");
            System.out.println("=========================================================================");
            System.out.println("The conf-files has been loaded.");
            System.out.println("[init.config-path] : " + CONF_PATH);
            Properties props = new Properties();
            InputStream is = new FileInputStream(CONF_PATH);
            props.load(is);
            is.close();
            Enumeration propKeys = props.keys();
            HashMap paramHm = new HashMap();
            while(propKeys.hasMoreElements()) 
            {
                String classId = (String)propKeys.nextElement();
                String className = props.getProperty(classId);
                if(classId.toLowerCase().endsWith(".param"))
                {
                    String key = classId.substring(0, classId.lastIndexOf("."));
                    paramHm.put(key, className);
                    System.out.println("[init param loading]:" + key + "\t -" + className);
                }
            }
            for(propKeys = props.keys(); propKeys.hasMoreElements();)
            {
                String classId = (String)propKeys.nextElement();
                String className = props.getProperty(classId);
                if(classId.toLowerCase().endsWith(".class"))
                {
                    System.out.println("[init Class Name]:" + className);
                    StartClass sc = (StartClass)Class.forName(className).newInstance();
                    sc.service(paramHm);
                } else
                if(!classId.toLowerCase().endsWith(".param"))
                {
                    System.out.println("[conf-files has some error] : \n");
                    System.out.println("[" + classId + " = " + className + "] is not supperted format.\nYou can use only two type suffix.(ex. *.class, *.param)");
                }
            }

            System.out.println("=========================================================================");
        }
        catch(Exception ex)
        {
            System.err.println("StartServlet StartClass Error : ");
            ex.printStackTrace();
        }
    }

    private static boolean initialized = false;

}
